Skip to content

perf: CUDA backend for PDF calculator and multi-threading support - #78

Open
SYMSCAE wants to merge 6 commits into
diffpy:mainfrom
SYMSCAE:cuda
Open

SYMSCAE wants to merge 6 commits into
diffpy:mainfrom
SYMSCAE:cuda

Conversation

@SYMSCAE

@SYMSCAE SYMSCAE commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

The problem

PDFCalculator and DebyePDFCalculator add pairs one at a time on a single
thread, so full PDF and Debye sums are slow for large structures and in
refinement loops. This PR speeds up those sums on the CPU and adds a CUDA backend.

CPU

  • Built-in calculators add all pairs through a new batched path,
    PairQuantity::addAllPairContributions(). Derived calculators can implement it
    or reuse addAllPairsOfPDF / addAllPairsOfDebyeSum.
  • Large sums (≥ 16k pairs) are split across threads, up to the max threads available, and the partial sums are merged in range order.
  • OPTIMIZED now estimates the cost of an incremental update against a full
    sum and runs the full sum when it is clearly cheaper (requires a 15% margin
    to absorb estimate error).

GPU

  • Adds cuda (fp64) and cuda-mixed (fp32 sampling, fp64 distances and sums)
    kernels.
  • CUDA only runs batched full sums.

Fixes

  • DebyePDFCalculator uses each site's own occupancy when several sites share
    an element.
  • Separate calculators can run concurrently on a shared read-only structure.
  • Copies of a calculator get independent evaluators.
  • OPTIMIZED recalculates when a change of adapter class changes the structure.

Benchmarks

image

SYMSCAE and others added 3 commits November 18, 2026 19:39
Group equivalent pairs and reuse neighbor lists across small structure
changes. Automatically sum all pairs for supported calculators and retain
pair-by-pair evaluation for custom models and partitioned runs. Choose between
an incremental update and a full sum based on the estimated work.

Use per-site occupancies in Debye sums, preserve valid updates across
adapter classes, and isolate calculator copies and concurrent evaluations.

Split large CPU pair batches across workers and merge their private sums
in range order. Choose one worker per 8192 pairs in large batches, up
to the hardware concurrency reported by the system.

Keep distinct floating-point distances and widths separate at cutoffs.
Track site order across incremental updates and honor site-dependent
scattering factors in derived Debye calculators.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.95441% with 447 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.60%. Comparing base (17f7c56) to head (9a1959f).

Files with missing lines Patch % Lines
src/tests/TestCalculationKernelCuda.cpp 8.53% 300 Missing ⚠️
src/tests/TestPairTable.cpp 96.08% 2 Missing and 25 partials ⚠️
src/diffpy/srreal/PQEvaluator.cpp 73.68% 6 Missing and 14 partials ⚠️
src/diffpy/srreal/kernels/PairTable.cpp 93.33% 2 Missing and 11 partials ⚠️
src/diffpy/srreal/scatteringfactordata.cpp 74.50% 5 Missing and 8 partials ⚠️
src/diffpy/srreal/BaseDebyeSum.cpp 92.95% 0 Missing and 10 partials ⚠️
...iffpy/srreal/kernels/CalculationKernelDispatch.cpp 61.53% 5 Missing and 5 partials ⚠️
src/tests/pair_test_helpers.hpp 87.09% 1 Missing and 7 partials ⚠️
src/tests/TestThreads.cpp 91.56% 3 Missing and 4 partials ⚠️
src/diffpy/srreal/kernels/CalculationKernelCPU.cpp 92.40% 0 Missing and 6 partials ⚠️
... and 13 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #78      +/-   ##
==========================================
- Coverage   87.04%   86.60%   -0.45%     
==========================================
  Files         147      156       +9     
  Lines        8376    10449    +2073     
  Branches     2051     2388     +337     
==========================================
+ Hits         7291     9049    +1758     
- Misses        625      868     +243     
- Partials      460      532      +72     
Files with missing lines Coverage Δ
src/diffpy/EventTicker.hpp 66.66% <100.00%> (+6.66%) ⬆️
src/diffpy/HasClassRegistry.ipp 65.75% <100.00%> (+0.42%) ⬆️
src/diffpy/srreal/AtomicStructureAdapter.hpp 100.00% <ø> (ø)
src/diffpy/srreal/BaseBondGenerator.cpp 95.23% <100.00%> (+0.04%) ⬆️
src/diffpy/srreal/BaseBondGenerator.hpp 100.00% <ø> (ø)
src/diffpy/srreal/BaseDebyeSum.hpp 100.00% <100.00%> (ø)
src/diffpy/srreal/ConstantPeakWidth.hpp 100.00% <ø> (ø)
src/diffpy/srreal/DebyePDFCalculator.cpp 93.52% <100.00%> (+4.30%) ⬆️
src/diffpy/srreal/DebyePDFCalculator.hpp 92.30% <ø> (ø)
src/diffpy/srreal/DebyeWallerPeakWidth.cpp 82.35% <100.00%> (+5.88%) ⬆️
... and 36 more

... and 10 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@SYMSCAE
SYMSCAE marked this pull request as ready for review November 19, 2026 05:56
@SYMSCAE

SYMSCAE commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@sbillinge PR ready for review. For CI, I wonder if we have gpu machines enabled? (I think they're paid if we use github ones, or we can use other cloud providers). Codecov is currently not happy since I switched cuda build off for ci.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant